﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Calcite.Classes
{
    public partial class Worksheet : Form
    {
        public Worksheet()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets the active WorksheetGridView instance on a Worksheet
        /// </summary>
        public WorksheetGridView ActiveWorksheetGridView {
            // Strategy is to determine the active sheet based on design time configuration
            // Uses a designable property on WorksheetGridView to avoid subclassing TabPage
            get
            {
                List<Control> tabControls = Misc.Functions.GetAllChildControls(
                    this.worksheetMainTabControl.SelectedTab);
                foreach (Control ctrl in tabControls)
                {
                    if (ctrl is WorksheetGridView)
                    {
                        WorksheetGridView wsgv = ctrl as WorksheetGridView;
                        if (wsgv.IsPrincipleGrid)
                        {
                            return wsgv;
                        }

                    }
                }
                return null;
            }
        }

        private void insertColumnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveWorksheetGridView != null)
            {
                this.ActiveWorksheetGridView.InsertColumnAtSelection();
            }
        }

        private void editColumnsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveWorksheetGridView != null)
            {
                this.ActiveWorksheetGridView.EditColumns();
            }
        }

        private void tabPage1_Enter(object sender, EventArgs e)
        {
            // Puts focus on DataGridView for immediate user input
            this.rawDataGridView.Focus();
        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            // Puts focus on DataGridView for immediate user input
            this.analysisGridView.Focus();
        }

        private void Worksheet_Load(object sender, EventArgs e)
        {
            // Bind all the WorksheetGridViews to their data sources. Used to get around 
            // the designer automatically creating columns from data binding done in the 
            // WorksheetGridView constructor
            List<Control> childControls = Misc.Functions.GetAllChildControls(this);
            foreach (Control ctrl in childControls) {
                if (ctrl is WorksheetGridView) {
                    WorksheetGridView wsgv = ctrl as WorksheetGridView;
                    wsgv.BindData();
                }
            }
            
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            // Put in code to refactor the context menu
        }

        private void insertRowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.ActiveWorksheetGridView != null)
            {
                this.ActiveWorksheetGridView.InsertRowAtSelection();
            }
        }


    }
}
